package cn.momoky.ccool.service;

import cn.momoky.ccool.mapper.FileMD5Mapper;
import cn.momoky.ccool.pojo.FileMD5;
import cn.momoky.ccool.utils.UUID;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class FileMD5Service {

    private final UUID uuid;
    private final FileMD5Mapper fileMD5Mapper;

    public void add(FileMD5 fileMD5) {
        fileMD5.setId(uuid.get());
        fileMD5Mapper.add(fileMD5);
    }

    public List<String> getMD5ByNodeIds(List<Long> nodeIds) {
        return fileMD5Mapper.getMD5ByNodeIds(nodeIds);
    }

    public String getMD5ByNodeId(Long nodeId) {
        List<Long> nodeIds = new ArrayList<>();
        nodeIds.add(nodeId);
        List<String> md5List = this.getMD5ByNodeIds(nodeIds);
        if (md5List.isEmpty()) return null;
        return md5List.get(0);
    }

    public void delByNodeIds(List<Long> nodeIds) {
        fileMD5Mapper.delByNodeId(nodeIds);
    }

    public void delByNodeId(Long nodeId) {
        List<Long> nodeIds = new ArrayList<>();
        nodeIds.add(nodeId);
        delByNodeIds(nodeIds);
    }
}
